home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 7 / BBS in a Box - Macintosh - Volume VII (BBS in a Box) (January 1993).iso / Files / Prog / N-P / PaintToolUnit.cpt / PTDemo.Pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1988-10-31  |  6.9 KB  |  287 lines  |  [TEXT/TPAS]

  1. Program PTDemo;
  2.  
  3. {$U-}
  4. {$R PTDemoRes}
  5. {$U PaintToolUnit}
  6.  
  7. Uses
  8.   MemTypes,QuickDraw,OSIntf,ToolIntf,PackIntf,
  9.   PaintToolUnit;
  10.   
  11. Const
  12.   AppleMenu     = 128;
  13.   FileMenu      = 129;
  14.   EditMenu      = 130;
  15.   ToolMenu      = 131;
  16.   PatternMenu   = 132;
  17.   PenSizeMenu   = 133;
  18.   
  19. Var
  20.   myMenus       : array [AppleMenu..PenSizeMenu] of MenuHandle;
  21.   SprayCurs     : CursHandle;
  22.   CrossCurs     : CursHandle;
  23.   theWindow     : WindowPtr;
  24.   GrowArea      : Rect;
  25.   CurrentTool   : Integer;
  26.   CurrentPat    : Integer;
  27.   CurrentSize   : Integer;
  28.  
  29. {###############################################################################}
  30.  
  31. Procedure ProcessMenu(codeWord : Longint);    { handle menu selections}
  32. Var
  33.   menuNum   : Integer;
  34.   itemNum   : Integer;
  35.   theStr    : Str255;
  36.   dummy     : Integer;
  37. Begin
  38.   if codeWord <> 0 then begin
  39.     menuNum := HiWord(codeWord);
  40.     itemNum := LoWord(codeWord);
  41.     case menuNum of { the different menus}
  42.       AppleMenu :
  43.         begin
  44.           if itemNum = 1 then {DoAbout}
  45.           else begin
  46.             GetItem(myMenus[AppleMenu],itemNum,theStr);
  47.             dummy:= OpenDeskAcc(theStr);
  48.           end;
  49.         end;
  50.       FileMenu : ExitToShell;
  51.       EditMenu : if NOT SystemEdit(itemNum - 1) then ;
  52.       ToolMenu :
  53.         begin
  54.           CheckItem(myMenus[ToolMenu],CurrentTool,false);
  55.           CurrentTool:= itemNum;
  56.           CheckItem(myMenus[ToolMenu],CurrentTool,true);
  57.         end;
  58.       PatternMenu :
  59.         begin
  60.           CheckItem(myMenus[PatternMenu],CurrentPat,false);
  61.           CurrentPat:= itemNum;
  62.           CheckItem(myMenus[PatternMenu],CurrentPat,true);
  63.           SetPort(theWindow);
  64.           case CurrentPat of
  65.             1 : PenPat(white);
  66.             2 : PenPat(ltGray);
  67.             3 : PenPat(gray);
  68.             4 : PenPat(dkGray);
  69.             5 : PenPat(black);
  70.           end
  71.         end;
  72.       PenSizeMenu :
  73.         begin
  74.           CheckItem(myMenus[PenSizeMenu],CurrentSize,false);
  75.           CurrentSize:= itemNum;
  76.           CheckItem(myMenus[PenSizeMenu],CurrentSize,true);
  77.           SetPort(theWindow);
  78.           case CurrentSize of
  79.             1 : PenSize(1,1);
  80.             2 : PenSize(2,2);
  81.             3 : PenSize(2,4);
  82.             4 : PenSize(4,2);
  83.             5 : PenSize(5,5);
  84.             6 : PenSize(10,2);
  85.             7 : PenSize(2,10);
  86.             8 : PenSize(10,10);
  87.           end
  88.         end;
  89.     end; {case}
  90.     HiliteMenu(0);
  91.   end; {big if}
  92. End;
  93.  
  94. {###############################################################################}
  95.  
  96. Procedure DealWithMouseDowns(theEvent: EventRecord);
  97. Var
  98.   whichWindow   : WindowPtr;
  99.   mouseLoc      : Point;
  100.   windowLoc     : Integer;
  101.   position      : LongInt;
  102.   dummy         : Integer;
  103. Begin
  104.   mouseLoc:= theEvent.where;
  105.   windowLoc:= FindWindow(mouseLoc,whichWindow);
  106.   case windowLoc of
  107.     inMenuBar   : ProcessMenu(MenuSelect(mouseLoc));
  108.     inSysWindow : SystemClick(theEvent,whichWindow);
  109.     inDrag      : DragWindow(whichWindow,mouseLoc,screenBits.bounds);
  110.     inZoomIn,inZoomOut :
  111.       begin
  112.         if TrackBox(whichWindow,mouseLoc,windowLoc) then begin
  113.           SetPort(whichWindow);
  114.           ClipRect(whichWindow^.portRect);
  115.           EraseRect(whichWindow^.portRect);
  116.           ZoomWindow(whichWindow,windowLoc,true);
  117.           InvalRect(whichWindow^.portRect);
  118.         end;
  119.       end;
  120.     inGrow :
  121.       begin
  122.         position:= GrowWindow(whichWindow,mouseLoc,GrowArea);
  123.         if position <> 0 then begin
  124.           SizeWindow(whichWindow,loword(position),hiword(position),false);
  125.           SetPort(whichWindow);
  126.           InvalRect(whichWindow^.portRect);
  127.         end;
  128.       end;
  129.     inGoAway :
  130.       begin
  131.         if TrackGoAway(whichWindow,mouseLoc) then ExitToShell;
  132.       end;
  133.     inContent :
  134.       begin
  135.         if whichWindow <> FrontWindow then
  136.           SelectWindow(whichWindow)
  137.         else begin
  138.           SetPort(whichWindow);
  139.           GlobalToLocal(mouseLoc);
  140.           DoPaintTools(CurrentTool,mouseLoc,theEvent.modifiers);
  141.           if PTError = TUnImp then
  142.             dummy:= Alert(128,nil)
  143.           else if PTError <> noErr then sysbeep(1);
  144.         end;
  145.       end;
  146.   end;
  147. End;
  148.  
  149. Procedure DealWithKeyDowns(theEvent: EventRecord);
  150. Var
  151.   CharCode  : char;
  152. Begin
  153.   CharCode := CHR(BitAnd(theEvent.message,charCodeMask));
  154.   if BitAnd(theEvent.modifiers,CmdKey) = CmdKey
  155.     then ProcessMenu(MenuKey(CharCode));
  156. End;
  157.  
  158. Procedure DealWithActivates(theEvent: EventRecord);
  159. Var
  160.   TargetWindow    : WindowPtr;
  161. Begin
  162.   TargetWindow := WindowPtr(theEvent.message);
  163.   if Odd(theEvent.modifiers)
  164.     then SetPort(TargetWindow);
  165. End;
  166.  
  167. Procedure DealWithUpdates(theEvent: EventRecord);
  168. Var
  169.   UpDateWindow  : WindowPtr;
  170.   tempPort      : WindowPtr;
  171. Begin
  172.   UpDateWindow := WindowPtr(theEvent.message);
  173.   GetPort(tempPort);
  174.   SetPort(UpDateWindow); 
  175.   BeginUpDate(UpDateWindow);
  176.     ClipRect(UpDateWindow^.portRect);
  177.     EraseRect(UpDateWindow^.portRect);
  178.     PenSize(5,5);
  179.     FrameOval(UpDateWindow^.portRect);
  180.     PenSize(1,1);
  181.   EndUpDate(UpDateWindow);
  182.   SetPort(tempPort);
  183. End;
  184.  
  185. Procedure AdjustCursor;
  186. Var
  187.   where : Point;
  188. Begin
  189.   GetMouse(where);
  190.   if (theWindow = FrontWindow) AND PtInRect(where,theWindow^.portRect) then
  191.     case CurrentTool of
  192.       TSpray    : SetCursor(SprayCurs^^);
  193.       TLine     : SetCursor(CrossCurs^^);
  194.     end
  195.   else
  196.     InitCursor;
  197. End;
  198.  
  199. Procedure MainEventLoop;
  200. Var
  201.   Event        : EventRecord;
  202. Begin
  203.   repeat
  204.     AdjustCursor;
  205.     SystemTask;
  206.     if GetNextEvent(everyEvent, Event) then
  207.       case Event.what of
  208.         mouseDown   : DealWithMouseDowns(Event);
  209.         AutoKey     : DealWithKeyDowns(Event);
  210.         KeyDown     : DealWithKeyDowns(Event);
  211.         ActivateEvt : DealWithActivates(Event);
  212.         UpdateEvt   : DealWithUpdates(Event);
  213.       end; {case}
  214.   until FALSE;
  215. End;
  216.  
  217. {###############################################################################}
  218.  
  219. Procedure SetupMacintosh;
  220. Var
  221.   theWatch  : CursHandle;
  222. Begin
  223.   MaxApplZone;
  224.   MoreMasters;
  225.   MoreMasters;
  226.   MoreMasters;
  227.  
  228.   FlushEvents(everyEvent,0);
  229.  
  230.   InitGraf(@thePort);
  231.   InitFonts;
  232.   InitWindows;
  233.   InitMenus;
  234.   TEInit;
  235.   InitDialogs(nil);
  236.   
  237.   theWatch:= GetCursor(watchCursor);
  238.   SetCursor(theWatch^^);
  239. End;
  240.  
  241. Procedure SetupMenus;
  242. Var
  243.   index    : Integer;
  244. Begin
  245.   for index:= AppleMenu to PenSizeMenu do begin
  246.     myMenus[index] := GetMenu(index);
  247.     InsertMenu(myMenus[index],0);
  248.   end;
  249.   AddResMenu(myMenus[AppleMenu],'DRVR');
  250.   DrawMenuBar;
  251. End;
  252.  
  253. Procedure SetupStuff;
  254. Var
  255.   tempRect  : Rect;
  256.   version   : Integer;
  257. Begin
  258.   version:= InitPaintTools;
  259.  
  260.   with screenBits.bounds do
  261.     SetRect(GrowArea,200,200,right,bottom);
  262.     
  263.   SetRect(tempRect,50,50,400,300);
  264.   theWindow:= NewWindow(nil,tempRect,'untitled',true,8,pointer(-1),true,0);
  265.   SetPort(theWindow);
  266.   
  267.   CurrentTool:= TPencil;
  268.   CurrentPat:= 5;
  269.   CurrentSize:= 1;
  270.   CheckItem(myMenus[ToolMenu],CurrentTool,true);
  271.   CheckItem(myMenus[PatternMenu],CurrentPat,true);
  272.   CheckItem(myMenus[PenSizeMenu],CurrentSize,true);
  273.   
  274.   SprayCurs:= GetCursor(128);
  275.   CrossCurs:= GetCursor(crossCursor);
  276. End;
  277.  
  278. Begin
  279.   SetupMacintosh;
  280.  
  281.   SetupMenus;
  282.   SetupStuff;
  283.   InitCursor;
  284.   
  285.   MainEventLoop;
  286. End.
  287.